using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using JSON;

public class JSONMusicData{
	#region 单例
	private static JSONMusicData instance;
	public static JSONMusicData getInstance(){
	if (instance == null)
		instance = new JSONMusicData ();
		return instance;
	}
	#endregion

	#region 变量
	private JSONNode json;
	private JSONNode JSONData{
		get{
			if(json == null)
				json = JsonRead.Read("JSONMusicData");
			return json;
		}
	}
	#endregion
	#region 外部调用
	public void Clear(){
		json = null;
	}
	public JSONNode GetJSON(string key){
		return JSONData [key];
	}
	public JSONNode GetJSON(int index){
		return JSONData [index];
	}
	/// <summary>
	/// 有多少个数据
	/// </summary>
	public int GetCount(){
	return JSONData.Count;
	}

    /// <summary>
	/// GetJSONNodeList Like Target
	/// </summary>
	public int[] GetListMusicName(string target){
		List<int> list = new List<int>();
		for (int i = 0; i < GetCount(); i++) {
			if( GetMusicName(i) == target)
				list.Add(i);
		}
		return list.ToArray();
	}
	/// <summary>
	/// 音乐名称
	/// </summary>
	public string GetMusicName(string key){
		JSONNode json = GetJSON (key);
		string  value;
		value = json["MusicName"].ToString();
		return  value;
	}
	/// <summary>
	/// 音乐名称 :From Index
	/// </summary>
	public string GetMusicName(int index){
		string value;
		JSONNode json = GetJSON (index);
		if(json == null){
			value = GetMusicName(index.ToString());
			return  value;
		}
		value = json["MusicName"].ToString();
		return  value;
	}
    /// <summary>
	/// GetJSONNodeList Like Target
	/// </summary>
	public int[] GetListPrefabName(string target){
		List<int> list = new List<int>();
		for (int i = 0; i < GetCount(); i++) {
			if( GetPrefabName(i) == target)
				list.Add(i);
		}
		return list.ToArray();
	}
	/// <summary>
	/// 预制名称
	/// </summary>
	public string GetPrefabName(string key){
		JSONNode json = GetJSON (key);
		string  value;
		value = json["PrefabName"].ToString();
		return  value;
	}
	/// <summary>
	/// 预制名称 :From Index
	/// </summary>
	public string GetPrefabName(int index){
		string value;
		JSONNode json = GetJSON (index);
		if(json == null){
			value = GetPrefabName(index.ToString());
			return  value;
		}
		value = json["PrefabName"].ToString();
		return  value;
	}
    /// <summary>
	/// GetJSONNodeList Like Target
	/// </summary>
	public int[] GetListLoop(int target){
		List<int> list = new List<int>();
		for (int i = 0; i < GetCount(); i++) {
			if( GetLoop(i) == target)
				list.Add(i);
		}
		return list.ToArray();
	}
	/// <summary>
	/// 循环 (1=循环,0=不循环) 
	/// </summary>
	public int GetLoop(string key){
		JSONNode json = GetJSON (key);
		int  value;
		value = json["Loop"].AsInt;
		return  value;
	}
	/// <summary>
	/// 循环 (1=循环,0=不循环)  :From Index
	/// </summary>
	public int GetLoop(int index){
		int value;
		JSONNode json = GetJSON (index);
		if(json == null){
			value = GetLoop(index.ToString());
			return  value;
		}
		value = json["Loop"].AsInt;
		return  value;
	}
    /// <summary>
	/// GetJSONNodeList Like Target
	/// </summary>
	public int[] GetListMusicVolume(float target){
		List<int> list = new List<int>();
		for (int i = 0; i < GetCount(); i++) {
			if( GetMusicVolume(i) == target)
				list.Add(i);
		}
		return list.ToArray();
	}
	/// <summary>
	/// 音量大小 (介于0-1之间) 
	/// </summary>
	public float GetMusicVolume(string key){
		JSONNode json = GetJSON (key);
		float  value;
		value = json["MusicVolume"].AsFloat;
		return  value;
	}
	/// <summary>
	/// 音量大小 (介于0-1之间)  :From Index
	/// </summary>
	public float GetMusicVolume(int index){
		float value;
		JSONNode json = GetJSON (index);
		if(json == null){
			value = GetMusicVolume(index.ToString());
			return  value;
		}
		value = json["MusicVolume"].AsFloat;
		return  value;
	}

	#endregion
}